<template>
  <div id="app-container">
    <!--条件查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="coach.name" placeholder="教练名字"/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList(-1)">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
      <router-link :to="'/account/coachAdd/'">
        <el-button type="default" size="default" >新增教练</el-button>
      </router-link>
    </el-form>
    <!--数据显示列表-->
    <el-table ref="multipleTable" :data="dataList" border tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="90">
        <template slot-scope="scope">
          {{pageSize*(currentPage-1)+scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="100" ></el-table-column>
      <el-table-column prop="sex" label="性别 " width="100" >
        <template slot-scope="scope">
          <span v-if="scope.row.sex==0">男</span>
          <span v-if="scope.row.sex==1">女</span>
        </template>
      </el-table-column>
      <el-table-column prop="tel" label="电话" width="150" ></el-table-column>
      <el-table-column prop="gymName" label="场馆名字" width="100" ></el-table-column>
      <el-table-column prop="status" label="状态 " width="100" >
        <template slot-scope="scope">
          <span v-if="scope.row.status==0">不允许</span>
          <span v-if="scope.row.status==1">允许</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <router-link :to="'/account/coachDetail/'+scope.row.id">
            <el-button type="success" size="mini" icon="el-icon-edit">查看</el-button>
          </router-link>
          <router-link :to="'/account/coachEdit/'+scope.row.id">
            <el-button type="success" size="mini" icon="el-icon-edit">编辑</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button @click="toggleSelection([dataList[1], dataList[2]])">切换第二、第三行的选中状态</el-button>
      <el-button @click="toggleSelection()">取消选择</el-button>
      <el-button @click="deleteAll()">删除所选</el-button>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 2, 3, 5]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import coach from "../../../api/coach";
export default {
  data(){
    return {
      dataList:[],
      multipleSelection: [],
      total:100,
      currentPage: 1,
      pageSize: 3,
      coach:{},
      type:{},
      sorts:[]
    }
  },
  created() {
    this.getList();
  },
  methods:{
    getList(row){
      console.log(this.coach.name)
      if (row==-1){
        this.currentPage=1;
      }
      coach.getList(this.currentPage,this.pageSize,this.coach.name).then(res=>{
        this.dataList=res.data.data;
        this.total=res.data.total;
      })
    },
    resetData(){
      this.coach={};
    },
    handleDelete(id){
      this.$confirm("是否确定删除数据？", "提示", {type: 'warning'})
        .then(() => {
          coach.delete(id).then(res=>{
            this.$message.success(res.message);
            this.currentPage = (this.total-1) % this.pageSize == 0 ? this.currentPage-1 : this.currentPage
            //当页数小于为0 就给它为1页 否则就还是当前页
            this.currentPage = this.currentPage < 1 ? 1 : this.currentPage
            this.getList();
          })
        })
        .catch(() => {
          // 表示点击了取消
          this.$message.info("取消删除操作");
        });
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize
      this.getList();
    },
    handleCurrentChange(currentPage){
      this.currentPage=currentPage;
      this.getList();
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
}
</script>
